﻿using Jizsoft.JFrame.Result;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jizsoft.JFrame.Modeling.Domain.Services
{
    public class QueryService
    {
        public PagedResult<Entities.FieldType> QueryFieldTypeList(Guid categoryId, int start, int limit)
        {
            using(JFDbContext dbContext = new JFDbContext())
            {
                var items = dbContext.Set<Entities.FieldType>().Where(p=>p.CategoryId == categoryId).OrderBy(p => p.Code).Skip(start).Take(limit).ToList();
                var total = dbContext.Set<Entities.FieldType>().Count(p => p.CategoryId == categoryId);
                return new PagedResult<Entities.FieldType>(start, limit, total, items);
            }
        }

        public ListResult<Entities.FieldTypeCategory> QueryFieldTypeCategoryList()
        {
            using (JFDbContext dbContext = new JFDbContext())
            {
                var items = dbContext.Set<Entities.FieldTypeCategory>().OrderBy(p => p.SortIndex).ToList();
                return new ListResult<Entities.FieldTypeCategory>(items);
            }
        }

        public ListResult<Entities.DataType> QueryDataTypeList()
        {
            using (JFDbContext dbContext = new JFDbContext())
            {
                var items = dbContext.Set<Entities.DataType>().OrderBy(p => p.Code).ToList();
                return new ListResult<Entities.DataType>(items);
            }
        }

        public PagedResult<Entities.Model> QueryUnpublishedModelList(int start, int limit)
        {
            using (JFDbContext dbContext = new JFDbContext())
            {
                var items = dbContext.Set<Entities.Model>().Where(p => p.PublishedTime == null && p.IsDeleted == false).OrderBy(p => p.Code).Skip(start).Take(limit).ToList();
                var total = dbContext.Set<Entities.Model>().Count(p => p.PublishedTime == null && p.IsDeleted == false);
                return new PagedResult<Entities.Model>(start, limit, total, items);
            }
        }

        public PagedResult<Entities.Model> QueryPublishedModelList(int start, int limit)
        {
            using (JFDbContext dbContext = new JFDbContext())
            {
                var items = dbContext.Set<Entities.Model>().Where(p => p.PublishedTime!=null && p.IsDeleted == false).OrderBy(p => p.Code).Skip(start).Take(limit).ToList();
                var total = dbContext.Set<Entities.Model>().Count(p => p.PublishedTime != null && p.IsDeleted == false);
                return new PagedResult<Entities.Model>(start, limit, total, items);
            }
        }

        public ListResult<Entities.ModelField> QueryModelFieldList(Guid modelId)
        {
            using (JFDbContext dbContext = new JFDbContext())
            {
                var items = dbContext.Set<Entities.ModelField>().Where(p => p.IsDeleted == false && p.ModelId == modelId).OrderBy(p => p.SortIndex).ToList();
                return new ListResult<Entities.ModelField>(items);
            }
        }
        public ListResult<Views.ModelRelationDetail> QueryModelRelationList(Guid modelId)
        {
            using (JFDbContext dbContext = new JFDbContext())
            {
                var items = dbContext.Set<Views.ModelRelationDetail>().Where(p => p.Model1Id == modelId).OrderBy(p => p.CreatedTime).ToList();
                return new ListResult<Views.ModelRelationDetail>(items);
            }
        }
    }
}
